using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using WorkflowServices = ScenarioFramework.WorkflowEngine.DataModel.WorkflowServices;

namespace ACEMPortal.Administration.Services
{
	/// <summary>
	/// Summary description for ServiceDetail.
	/// </summary>
	public partial class ServiceDetail : System.Web.UI.Page
	{

		protected ACEMPortal.Administration.UserControls.UserRoleSelection UserRoleSelection1;

		private void BindGrants(string sServiceName)
		{
			ScenarioFramework.WAPI.Response wapiResponse = new ScenarioFramework.WAPI.Response();
			string sAnswer = ScenarioFramework.WAPI.Service.GetServices (ref wapiResponse);
			WorkflowServices.XMLServices xmlServices= new ScenarioFramework.WorkflowEngine.DataModel.WorkflowServices.XMLServices();

			if (wapiResponse.iCode==0)
			{
				xmlServices.Parse(sAnswer);


			}
			else
			{
				Response.Redirect( SFPortal.Redirect.GoError ("TODOLIST - Unable to Load Services'") );
			}

			WorkflowServices.XMLService xmlService = new ScenarioFramework.WorkflowEngine.DataModel.WorkflowServices.XMLService();
			bool bfound = false;
			for ( int i=0; i<xmlServices.Count;i++)
			{
				xmlService = (WorkflowServices.XMLService)xmlServices[i];
				if ( xmlService.Name.CompareTo (sServiceName) == 0 )
				{
					bfound = true;
					break;
				}
			}

			if (bfound) 
			{
				UserRoleSelection1.XMLRoles = xmlService.XMLRoles.GetXML();
				UserRoleSelection1.XMLUsers = xmlService.XMLUsers.GetXML();
				this.txtDescription.Text = xmlService.Description;

				Session["XMLService"] = xmlService;
			}
			else
			{
				Response.Redirect( SFPortal.Redirect.GoError("CONFIGURATION MANAGER - Unable to bind procedure"));
			}
		}


		protected void Page_Load(object sender, System.EventArgs e)
		{
			if ( !Page.IsPostBack )
			{
				string sServiceName = Request.QueryString["id"];
				lblName.Text = sServiceName;

				BindGrants(sServiceName);
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnBack_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("ServiceList.aspx");
		}

		protected void btnPublish_Click(object sender, System.EventArgs e)
		{
			WorkflowServices.XMLService xmlService = 
				(WorkflowServices.XMLService)Session["XMLService"];

			xmlService.Description = this.txtDescription.Text;

			string sRoles = this.UserRoleSelection1.RolesIn();
			if ( sRoles.CompareTo ( string.Empty ) != 0  )
			{
				xmlService.XMLRoles.Parse (sRoles);
			}

			string sUsers = this.UserRoleSelection1.UsersIn();
			if ( sUsers.CompareTo ( string.Empty ) != 0 )
			{
				xmlService.XMLUsers.Parse (sUsers);
			}

			try
			{
				ScenarioFramework.WAPI.Response response = new ScenarioFramework.WAPI.Response();
				ScenarioFramework.WAPI.Service.AddService(xmlService.GetXML (), ref response);
				if ( response.iCode != 0 )
				{
					//trying to delete it for rewrite service if already exists.
					response.iCode = 0 ;
					string sError = response.sMsg;
					ScenarioFramework.WAPI.Service.DeleteService (xmlService.Name, ref response);
					if ( response.iCode == 0 )
					{
						ScenarioFramework.WAPI.Service.AddService(xmlService.GetXML (), ref response);
						if ( response.iCode != 0 )
						{
							throw new Exception(response.sMsg);
						}
					}
					else
						throw new Exception(sError);

				}
			}
			catch ( Exception err)
			{
				SFPortal.Redirect.GoError (err.Message);
			}

			Response.Redirect("ServiceList.aspx");

			
		}
	}
}
